package weaver.aiyh_pcn.async_organization;

import aiyh.utils.zwl.common.ToolUtil;
import weaver.aiyh_pcn.async_organization.model.Department;
import weaver.aiyh_pcn.async_organization.model.Employee;
import weaver.aiyh_pcn.async_organization.model.Position;
import weaver.aiyh_pcn.async_organization.result.GetOrganizationResult;
import weaver.aiyh_pcn.async_organization.util.SyncOrganizationUtils;
import com.weaver.esb.server.cache.ResourceComInfo;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.hrm.resource.HrmSynDAO;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.hrm.*;
import weaver.matrix.MatrixUtil;

import java.util.*;


/**
 * @author EBU7-dev1-ayh
 * @create 2021/7/20 0020 15:16
 * 同步第三方系统人员组织结构
 */


public class SyncOrganizationForOtherAPI extends ToolUtil implements HrmSynService {
	private HashMap<String,Object> synResult;
	String className = "SyncOrganizationForOtherAPI";
	private final Logger log = LoggerFactory.getLogger(SyncOrganizationForOtherAPI.class);
	private final GetOrganizationResult getOrganizationResult = new GetOrganizationResult();
	private final SyncOrganizationUtils organizationUtils = new SyncOrganizationUtils();

	public SyncOrganizationForOtherAPI(){
		this.writeDebuggerLog(className,"===========> create Object! <============");
		this.synResult = new HashMap<>();
	}
	/**同步到分公司(分部)*/
	@Override
	public String SynTimingToOASubCompany() {
		this.writeDebuggerLog(className,"===========> synchronous company to OA system start <============");
		this.synResult.put("1",null);
		this.writeDebuggerLog(className,"===========> synchronous company to OA system end <============");
		return null;
	}

	/**同步到部门*/
	@Override
	public String SynTimingToOADepartment() {
		this.writeDebuggerLog(className,"===========> synchronous department to OA system starts <============");
		List<Department> departmentList = getOrganizationResult.getDepartmentList();
		Collections.sort(departmentList);
		List<Map<String,Object>> synResultlist = new ArrayList<>();
		departmentList.forEach(department -> {
			Map<String, String> stringStringMap = organizationUtils.asyncDepartment(department);
			synResultlist.add(buildItemMap("","",department.getDEPARTMENTNAME(),stringStringMap.get("code"),stringStringMap.get("msg")));
		});
		this.writeDebuggerLog(className,departmentList.size() +" data pieces are updated this time");
		//清除OA中分部的缓存记录
		SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
		subCompanyComInfo.removeCompanyCache();
		subCompanyComInfo.removeCache();
		//清除部门缓存
		DepartmentComInfo departmentComInfo = new DepartmentComInfo();
		departmentComInfo.removeCompanyCache();
		departmentComInfo.removeCache();

		//同步部门数据到矩阵
		MatrixUtil.sysDepartmentData();

		this.writeDebuggerLog(className,"This time together step or update data " + departmentList.size());
		this.synResult.put("2",synResultlist);
		this.writeDebuggerLog(className,"===========> synchronous department to OA system end <============");
		return null;
	}

	/**同步到职位*/
	@Override
	public String SynTimingToOAJobtitle() {
		this.writeDebuggerLog(className,"===========> synchronous job to OA system starts <============");
		List<Position> positionList = getOrganizationResult.getPositionList();
		Collections.sort(positionList);
		List<Map<String,Object>> synResultlist = new ArrayList<>();
		positionList.forEach(position->{
			Map<String, String> stringStringMap = organizationUtils.asyncPosition(position);
			synResultlist.add(buildItemMap(position.getJOBCODE(),position.getJOBCODE(),position.getJOBFUNCTION(),stringStringMap.get("code"),stringStringMap.get("msg")));
		});
		this.writeDebuggerLog(className,positionList.size() +" data pieces are updated this time");
//		 清除职位缓存
		JobTitlesComInfo jobTitlesComInfo = new JobTitlesComInfo();
		jobTitlesComInfo.removeJobTitlesCache();
		jobTitlesComInfo.removeCache();
		this.synResult.put("3",synResultlist);
		this.writeDebuggerLog(className,"===========> synchronous job to OA system end <============");
		return null;
	}

	/**同步到人员*/
	@Override
	public String SynTimingToOAHrmResource() {
		this.writeDebuggerLog(className,"===========> synchronous hrm to OA system starts <============");
		List<Employee> employeeList = getOrganizationResult.getEmployeeList();
		List<Map<String,Object>> synResultlist = new ArrayList<>();
		Collections.sort(employeeList);
		employeeList.forEach(employee->{
			Map<String, String> stringStringMap = organizationUtils.asyncEmployee(employee);
			synResultlist.add(buildItemMap(employee.getUSERCODE(),employee.getUSERCODE(),employee.getPreferred_Name() + "/" + employee.getUSERNAMECN(),stringStringMap.get("code"),stringStringMap.get("msg")));
		});
		this.writeDebuggerLog(className,employeeList.size() +" data pieces are updated this time");
		// 清除人员缓存
		try {
			ResourceComInfo rsc = new ResourceComInfo();
			rsc.removeCache();
		} catch (Exception e) {
			this.writeErrorLog(className,"removeCache error!");
			e.printStackTrace();
		}
//		organizationUtils.asyncEmployee(employeeList.get(0));
		this.synResult.put("4", synResultlist);
		this.writeDebuggerLog(className,"===========> synchronous hrm to OA system end <============");
		return null;
	}

	@Override
	public void SynTimingFromOASubCompany(SubCompanyBean[] subCompanyBeans) {

	}

	@Override
	public void SynTimingFromOADepartment(DepartmentBean[] departmentBeans) {

	}

	@Override
	public void SynTimingFromOAJobtitle(JobTitleBean[] jobTitleBeans) {

	}

	@Override
	public void SynTimingFromOAHrmResource(UserBean[] userBeans) {

	}

	@Override
	public void SynInstantSubCompany(SubCompanyBean subCompanyBean) {

	}

	@Override
	public void SynInstantDepartment(DepartmentBean departmentBean) {

	}

	@Override
	public void SynInstantJobtitle(JobTitleBean jobTitleBean) {

	}

	@Override
	public void SynInstantHrmResource(UserBean userBean) {

	}

	@Override
	public boolean SynSendMessage(String s, String s1, String s2, String s3, String s4) {
		return false;
	}

	@Override
	public HashMap<String,Object> getSynResult() {
		return this.synResult;
	}

	@Override
	public void removeSynResult() {
		this.synResult = new HashMap();
	}

	private Map<String,Object> buildItemMap(String pkcode, String pk, String memo, String succ, String error){
		//保存结果
		HashMap<String,Object> itemMap = new HashMap<>();
		itemMap.put(weaver.hrm.resource.HrmSynDAO.OUTPK, pkcode);
		itemMap.put(weaver.hrm.resource.HrmSynDAO.PK, pk);
		itemMap.put(weaver.hrm.resource.HrmSynDAO.Memo, memo);
		itemMap.put(weaver.hrm.resource.HrmSynDAO.Success, succ);
		itemMap.put(HrmSynDAO.ErrorMessage, error);
		return itemMap;
	}
}
